﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;

namespace TireService.Website
{
    public class HelperFunctions
    {
        /// <summary>
        /// Helper function that returns longitude and latitude coordinates for a specific address from google maps 
        /// </summary>
        /// <param name="Address">Address to return coordinates for</param>
        /// <param name="MapKey">Google Maps API key</param>
        /// <returns>Double array with longitude and latitude</returns>
        public static double[] GetLongLatCoordinates(string Address, string MapKey)
        {
            GMapGeocoder.Containers.Results coordinatesSecondary = new GMapGeocoder.Containers.Results();
            coordinatesSecondary = GMapGeocoder.Util.Geocode(Address, MapKey);
            double[] LongLatCoordinates = new double[2];
            LongLatCoordinates[0] = (double)coordinatesSecondary.Addresses[0].Coordinates.Longitude / 10000000;
            LongLatCoordinates[1] = (double)coordinatesSecondary.Addresses[0].Coordinates.Latitude / 10000000;

            return LongLatCoordinates;
        }

        public static NameValueCollection OnlinePayment(string Amount, string InvoiceId, string ShopCurrency, string ShopMd5Key, string ShopId, string ShopOrderPrefix, string ShopPayType, string ShopCardType, string ShopLanguage, string AcceptUrl, string DeclineUrl)
        {
            string amount = string.Format("{0:N}", Amount).Replace(".", "").Replace(",", "");
            string md5sum = amount + InvoiceId + ShopCurrency + ShopCurrency + ShopMd5Key;
            //string acceptURL = "www.mobilhjulskift.dk/Betal.aspx?Id=" + InvoiceGuid + "Action=1";
            //string declineURL = "www.mobilhjulskift.dk/BetalFejl.aspx?Id=" + InvoiceGuid;


            //Encrypt the md5
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] hashedBytes;
            UTF8Encoding encoder = new UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(md5sum));
            string checkmd5 = Convert.ToString(hashedBytes);

            NameValueCollection data = new NameValueCollection();
            data.Add("shopid", ShopId);
            data.Add("currency", ShopCurrency);
            data.Add("amount", amount);
            data.Add("orderid", InvoiceId);
            data.Add("orderidprefix", ShopOrderPrefix);
            data.Add("paytype", ShopPayType);
            data.Add("cardtype", ShopCardType);
            data.Add("lang", ShopLanguage);
            data.Add("uniqorderid", "true");
            data.Add("accepturl", AcceptUrl);
            data.Add("declineurl", DeclineUrl);
            //data.Add("checkmd5", checkmd5.ToString());

            return data;
        }

        public static NameValueCollection PaypalOnlinePayment(string CustomerId, string ReturnURL, string CancelURL, string BusinessEmail, string CompanyLogo, string BuyerEmail, string CurrencyCode, string ItemName, string Amount, string InvoiceGuid, string InvoiceId)
        {
            NameValueCollection data = new NameValueCollection();
            data.Add("cmd", "_xclick");
            data.Add("business", BusinessEmail);
            data.Add("email", BuyerEmail);
            data.Add("currency_code", CurrencyCode);
            data.Add("image_url", CompanyLogo);
            data.Add("item_name", ItemName);
            data.Add("amount", Amount);
            data.Add("invoice", InvoiceId);
            data.Add("on0", "CustomerId");
            data.Add("os0", CustomerId);
            data.Add("return", ReturnURL + "?Paypal=Success&Id=" + InvoiceGuid);
            data.Add("cancel_return", CancelURL + "?Paypal=Cancel&Id=" + InvoiceGuid);
            //data.Add("notify_url", NotifyURL);

            return data;
        }
    }
}